// pages/visit/list/list.js
var visitservice = require("../../../apiservice/visit/visitservice");
var jsondata = require("../jsondata.js");
var utils = require("../../../utils/util.js");
var pageSize = 20;
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		hasNextPage: true,
		contactname:'',
		visitList: [],
		startTime: '',
		endTime: '',
		scroll_height: 0,
		districtid: '',
		contactid: '',
		//sroll-view
		sliderLeft: 0,
		sliderOffset: 0,
		height: 0,
		activeIndex: 0,
		tabs: ["全部", "发布", "进行中", "暂停", "完成"],
		page: 1, //首页页数，默认是1
		statecode: null, //当前状态值
		tabIndex: 0 //记录tab的坐标，默认是0
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
			title: '加载中',
			icon: 'loading'
		});
		if (options.contactid) {
			//console.log(options.contactid)
			this.setData({
				contactid: options.contactid,
				contactname:options.contactname,
			})
		}
		if (options.districtid) {
			this.setData({
				contactid: options.contactid
			})
		}

		this.rollBackParams();
		//设置当前时间，用于初始化查询
		var currentDate = utils.getCurrentMonthDate();
		this.setData({
			startTime: currentDate.starttime,
			endTime: currentDate.endtime
		});
		var that = this;
		wx.getSystemInfo({
			success: function (res) {

				that.setData({
					sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
					sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
					height: res.windowHeight,
					screenheight: res.screenHeight
				});

				wx.hideLoading();
			}
		});

	},
	/**
	 * 跳转到添加页面
	 */
	addVisit: function () {
		wx.navigateTo({
			url: '/pages/visit/add/add'
		})
	},
	/**
	 * 根据状态获取页面
	 * @param {*} e 
	 */
	onTabChange: function (e) {
		var tabIndex = e.detail.index; //状态坐标
		//还原参数，防止页码混乱，导致数据错乱
		this.rollBackParams();
		var statecode = null;
		if (tabIndex == 0) { 
      //进行中
			statecode = null;
		} else if (tabIndex == 1) {
      //已完成
      statecode = 1;
		} else if (tabIndex == 2) {
      //已取消
      statecode = 2;
		}
		this.setData({
			statecode: statecode,
			tabIndex: tabIndex
		})
		this.getAppoinmentsByContactId(this.data.page,statecode);

	},
	/**
	 * 根据日期查询
	 * @param {*} e 
	 */
	onSearch: function (e) {
		//还原参数，防止页码混乱，导致数据错乱
		this.rollBackParams();
		this.setData({
			startTime: e.detail.starttime,
			endTime: e.detail.endtime
		});
		this.getAppoinmentsByContactId(this.data.page,this.data.statecode);
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		//console.log(this.data);
		//还原参数，防止页码混乱，导致数据错乱
		this.rollBackParams();
		//获取全部数据
		this.getAppoinmentsByContactId(this.data.page,this.data.statecode);

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},
	lower: function () {
		if(this.data.hasNextPage){
			var page =  this.data.page+1; 
			this.setData({
				page : page
			});
			this.getAppoinmentsByContactId(page,this.data.statecode,true);
			
		}else{
			wx.showToast({ //如果全部加载完成了也弹一个框
				title: '我也是有底线的',
				icon: 'success',
				duration: 350
			});
			return false;
		}
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},

	//根据拜访对象获取列表
	getAppoinmentsByContactId: function (page,statecode, islower) {
		var that = this;
		wx.showLoading({
			title: '数据正在加载中'
		})
		console.log( this.data.contactid);
		visitservice.GetAppointmentByContactId(statecode, this.data.startTime, this.data.endTime, this.data.contactid,
			this.data.districtid, page, pageSize).then(
				function (success) {
					//debugger;
					//console.log(success);
					that.setData({
						['visitList[' + (page - 1) + ']']: success.data.datas,
						hasNextPage : success.data.existsnextdata				
					});
					//渲染标签
					that.addStateName(page);
					//查询成功加页数
					//  if (islower) {
					// 	wx.showToast({ //如果全部加载完成了也弹一个框
					// 		title: '我也是有底线的',
					// 		icon: 'success',
					// 		duration: 350
					// 	});
					// }
					wx.hideLoading();
				}).catch(function (ex) {
					console.log(ex);
					wx.hideLoading();
				});
	},
	/**
	 * 渲染标签
	 */
	addStateName: function (page) {
		//var page = this.data.page;
		var array = this.data.visitList[page - 1];
		if (array == null || array.length == 0) {
			return;
		}
		array.forEach((r) => { //array是后台返回的数据
			var obj = jsondata.getStateNameByStateCode(r.statecode);
			r.stateName = obj.name; //r = array[0]的所有数据，这样直接 r.新属性 = 属性值 即可
			r.color = obj.color;
		})
		this.setData({
			['visitList[' + (page - 1) + ']']: array
		})
	},
	/**
	 * 复原参数,防止页码混乱
	 */
	rollBackParams: function () {

		this.setData({
			visitList: null, //还原数组
			hasNextPage: true,
			page: 1 //还原状态页码
		});
	}
})
